Imports System.Collections.Generic
Imports System.Data.SqlClient

Partial Class Admin_FormPermissions
    Inherits System.Web.UI.Page


    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim strFieldID As String
        Dim keys As DataKeyArray = FormPermissionsScrollingGridView.DataKeys
        Dim rolePermissionObject As Cdm.DataCore.Admin.RoleFieldPermission

        For i As Integer = 0 To Me.FormPermissionsScrollingGridView.Rows.Count - 1
            strFieldID = keys.Item(i).Values.Item(1).ToString()
            rolePermissionObject = Cdm.DataCore.Admin.RoleFieldPermission.Retrieve(Convert.ToInt32(Me.RolesDropDownList.SelectedValue), Convert.ToInt32(strFieldID))

            Dim chkBox As CheckBox
            chkBox = Me.FormPermissionsScrollingGridView.Rows(i).FindControl("IsEditableCheckBox")

            rolePermissionObject.IsEditable = chkBox.Checked
            rolePermissionObject.Persist()
        Next

        Me.RolesDropDownList.Attributes.Add("onchange", "document.getElementById(""mainSection"").style.visibility=""hidden"";document.getElementById(""saveSection"").style.visibility=""hidden"";")
        Me.FormPermissionsDropDownList.Attributes.Add("onchange", "document.getElementById(""mainSection"").style.visibility=""hidden"";document.getElementById(""saveSection"").style.visibility=""hidden"";")

        Me.FormFieldsObjectDataSource.SelectParameters.Item(0).DefaultValue = Me.RolesDropDownList.SelectedValue
        Me.FormFieldsObjectDataSource.SelectParameters.Item(1).DefaultValue = Me.FormPermissionsDropDownList.SelectedValue
        Me.FormFieldsObjectDataSource.Select()
        Me.FormPermissionsScrollingGridView.DataBind()
        SetStatus("Settings Saved.")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetupJavaScript()
        ClearStatus()
        If Not Page.IsPostBack Then

            Me.FormPermissionsScrollingGridView.Sort("FieldName", SortDirection.Ascending)

            Me.RolesObjectDataSource.Select()
            Me.FormPermissionsObjectDataSource.Select()

            Me.RolesDropDownList.DataBind()
            Me.RolesDropDownList.Items.Insert(0, "Please Select...")
            Me.RolesDropDownList.Items.Item(0).Value = "0"

            Me.FormPermissionsDropDownList.DataBind()
            Me.FormPermissionsDropDownList.Items.Insert(0, "Please Select...")
            Me.FormPermissionsDropDownList.Items.Item(0).Value = "0"

        End If

    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.FormPermissionsScrollingGridView.DivClientId, CInt(Me.FormPermissionsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.FormPermissionsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub

    Protected Sub FormPermissionsScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormPermissionsScrollingGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(FormPermissionsScrollingGridView)
    End Sub

    Protected Sub FormPermissionsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles FormPermissionsScrollingGridView.RowDataBound
        Dim chk As CheckBox = e.Row.FindControl("IsEditableCheckBox")
        Dim fieldName As String
        If chk IsNot Nothing Then
            fieldName = CType(e.Row.Cells(0).Controls(0), DataBoundLiteralControl).Text
            chk.InputAttributes.Add("title", "is " & fieldName & " editable?")
        End If
    End Sub
End Class
